package com.yw.controller;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.yw.dataStandardCatalog.domain.DataStandardCatalog;
import com.yw.dataStandardCatalog.dto.AddDataStandardCatalogDTO;
import com.yw.dataStandardCatalog.dto.DataStandardCatalogDTO;
import com.yw.dataStandardCatalog.dto.UpdateDataStandardCatalogDTO;
import com.yw.piliangDTO.StatusDTO;
import com.yw.result.PageDTO;
import com.yw.result.R;
import com.yw.service.DataStandardCatalogService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;


@Slf4j
@RestController
@CrossOrigin(origins = "*")  //解决跨域
@Tag(name = "数据标准目录",description = "数据标准目录")
@RequestMapping("/DataStandardCatalog")
public class DataStandardCatalogController {
    @Resource
    private DataStandardCatalogService dataStandardCatalogService;


    @Operation(summary = "条件查询数据标准目录",description = "条件查询数据标准目录")
    @PostMapping("/pageDatabaseByQuery")
    public R pageAllByQuery(@RequestBody DataStandardCatalogDTO dataStandardCatalogDTO){
        return R.OK(dataStandardCatalogService.pageAllByQuery(dataStandardCatalogDTO));
    }

    @Operation(summary = "根据标准编号得到表",description = "根据标准编号得到表")
    @PostMapping("/clickOnOne/{id}")
    public R ClickOnOne(@PathVariable Integer id){
        return R.OK(dataStandardCatalogService.ClickOnOne(id));
    }

    @Operation(summary = "新增数据标准",description = "新增数据标准")
    @PostMapping("/addDataStandard")
    public R addDataStandard(@RequestBody AddDataStandardCatalogDTO addDataStandardCatalogDTO){
        return dataStandardCatalogService.addDataStandard(addDataStandardCatalogDTO)?R.OK():R.FAIL();
    }
    @Operation(summary = "修改数据标准",description = "修改数据标准")
    @PostMapping("/updateDataStandard")
    public R updateDataStandard(@RequestBody UpdateDataStandardCatalogDTO updateDataStandardCatalogDTO){
        return dataStandardCatalogService.updateDataStandard(updateDataStandardCatalogDTO)?R.OK("修改成功"):R.FAIL("修改失败");
    }
    @Operation(summary = "删除数据标准",description = "删除数据标准")
    @PostMapping("/deleteDataStandard")//方法名应该为delete开头，复制错误
    public R deleteDataStandard(Integer id){
        return dataStandardCatalogService.removeById(id)?R.OK():R.FAIL();
    }//方法名应该为delete开头，复制错误

    @Operation(summary = "修改状态",description = "修改状态")
    @PostMapping("/updateStatic")
    public R updateStatic(Integer id){
        return dataStandardCatalogService.updateStatic(id)?R.OK():R.FAIL();
    }


    //    批量发布停用
    @Operation(summary = "批量发布停用")
    @PostMapping("/batch")
    public R batch(@RequestBody StatusDTO statusDTO){
        return dataStandardCatalogService.batch(statusDTO)?R.OK("批量发布成功"):R.FAIL("批量发布失败");
    }

    @Operation(summary = "    //数据资产管理中添加时点击标准对应的接口")
    @PostMapping("/all")
    public R all(){
        return R.OK(dataStandardCatalogService.all());
    }

    //文件上传
    @PostMapping(value = "/insertFile",consumes = "multipart/form-data")
    public R uploadExcel(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return R.FAIL("文件为空");
        }
        try {
            // 调用读取Excel的方法
            List<DataStandardCatalog> data = readExcel(file);
            dataStandardCatalogService.saveBatch(data);
            // 打印或处理读取的数据
            System.out.println(data);
            return R.OK("文件上传成功");
        } catch (IOException e) {
            e.printStackTrace();
            return R.FAIL("文件上传失败"+ e.getMessage());
        }
    }

    private List<DataStandardCatalog> readExcel(MultipartFile file) throws IOException {
        return EasyExcel.read(file.getInputStream(), DataStandardCatalog.class, new ReadListener<DataStandardCatalog>() {
            @Override
            public void invoke(DataStandardCatalog data, AnalysisContext context) {
                System.out.println("读取到的数据：" + data);
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext context) {
                System.out.println("所有数据读取完成！");
            }
        }).sheet().doReadSync();
    }
}
